Skip to content

Add vision-server: an HTTP server for the GGUF model families - #25

Open
iamskeole wants to merge 5 commits into
Acly:mainfrom
iamskeole:vision-server
Open

iamskeole wants to merge 5 commits into
Acly:mainfrom
iamskeole:vision-server

Conversation

@iamskeole

@iamskeole iamskeole commented Nov 22, 2026 •

Copy link
Copy Markdown

vision-server is a small HTTP/1.1 server in src/cli/ that serves the
project's GGUF model families over a minimal JSON API. Today the models are
only reachable from the command line (vision-cli); this adds a small door
for services that want to run them, with no new dependencies: POSIX sockets,
the project's util/string.h (gained base64), and a new self-contained
util/json.h.

API — images are base64 PNG or JPEG (data: URLs accepted):

Route Request Response
GET /health — server, backend and model status
POST /sam {"image", "point": {"x","y"} | "box": {"x1","y1","x2","y2"}} {"width","height","iou":[4],"masks":[4 base64 PNGs]} — all four heads, index-stable
POST /birefnet {"image"} {"width","height","image"} — base64 PNG alpha matte
POST /depth {"image"} {"width","height","image"} — base64 PNG depth map
POST /migan {"image","mask"} {"width","height","image"} — inpainted PNG
POST /esrgan {"image","tile"?} {"width","height","image"} — upscaled PNG

Design notes

  • Large POSTs use Expect: 100-continue; the server answers 100 Continue
    before reading the body (covered by a test). Header limit 64 KB, body
    limit 64 MB, both answered 4xx.
  • One in-flight inference at a time (a per-server mutex). This is a door for
    a single local client; a request queue is a reasonable follow-up.
  • SAM keeps a per-image encoder-output cache (hashed on the preprocessed
    pixels), so a stream of clicks/boxes on the same image skips TinyViT after
    the first. The encoder runs in a graph of its own (the predictor is a
    second graph that takes the embedding as an input): in this ggml the
    allocator may reuse a parent's buffer for a later in-place op, so the
    embedding can only be read back reliably from a graph where it is the
    terminal output. With the split, repeated requests for the same image are
    bit-identical (verified, CPU and Vulkan).
  • GPU co-residency: --max-arena <MB> (default 4096) is a per-request
    device-memory budget. It is probed at boot (the server fails with an
    actionable message if the device cannot provide it), and it is enforced
    before every graph allocation: this ggml-vulkan allocates one buffer per
    tensor, so a request that does not fit fails mid-allocation inside
    another client's headroom. The server instead dry-runs the graph
    allocator (ggml_gallocr_reserve_n_size, no allocation) to report exactly
    what the real allocation would need - in-place ops included - and refuses
    an over-budget request with 503 and its measured figure, before touching
    memory. The door fails loudly and stays a polite GPU tenant.
  • POSIX only for now (Linux/macOS; UNIX guard in CMake). The code uses only
    POSIX socket APIs, so a Windows port is mechanical.

Tests

  • tests/test-json.cpp (19): parse/write roundtrips, strict rejections
    (trailing data, leading zeros, depth limit, bad escapes, unpaired
    surrogates), base64 encode/decode.
  • tests/test-server.cpp (10): forks the real server on a random port, polls
    /health for readiness, then exercises /health, /sam point and box on
    a real image, inverted-box → 400, a real Expect: 100-continue roundtrip,
    bad JSON → 400, bad image → 400, unknown route → 404, unloaded model →
    404, and a server with a 16 MB budget refusing a request it cannot hold,
    with the measured size, instead of attempting the allocation.

All five families verified end-to-end over the API on both backends, one
server process, all five models resident:

  • CPU (Ryzen 5800X, with a 24 GB vLLM running on the GPU at the same
    time - zero VRAM touched): measured per-request graphs 188 MB (SAM), 1332 MB
    (BiRefNet lite), 742 MB (Depth V2 Small), 211 MB (MI-GAN), 217 MB (ESRGAN
    1024x1024 tile) - all inside the 4096 MB default budget; warm 129 ms (SAM
    point, cache hit) / 2.6-5.0 s for the rest. Semantic checks: the matte is
    bimodal, the depth map has full dynamic range, inpainting changes only the
    masked region, the upscale is 4x.
  • Vulkan (RTX 3090, card free): all five 200; warm 76-320 ms (SAM
    point/box 110 ms). Per-request budget as dry-run measured by the server:
    SAM 160 MB, BiRefNet 1327, Depth 303, MI-GAN 211, ESRGAN 218; peak with all
    five resident 694-2052 MiB. SAM per-head IoUs on a fixed reference click
    match the CPU server to < 0.01 per head (both backends run the same F16
    GGUFs), i.e. the Vulkan path introduces no quality delta.

Verified on Linux x86_64 (Ryzen 5800X, GGML_NATIVE=OFF SSE4.2/AVX2/F16C,
VISP_FMT_LIB=ON, GGML_BACKEND_DL=ON): ctest -C Release — all C++ tests
pass, including an AddressSanitizer build of the server under a sustained
point/box mix. No Vulkan-specific tests: the project's CI already runs
Vulkan off (llvmpipe is unreliable there) and this PR adds no Vulkan code.
The pre-existing
tests/test_birefnet.py::test_relative_position_index Python failure is
unrelated (it fails on main on the same machine).

One caveat for consumers: F16 GGUF outputs are not byte-identical to an
FP32/ONNX reference of the same model; compare by selection (IoU / which
head), not by pixels.

Happy to first discuss the shape of this in an issue if you prefer — the
branch is ready either way.

Signed-off-by: iamskeole <214513857+iamskeole@users.noreply.github.com>
Strict RFC 8259 parsing (rejects trailing data, leading zeros, deep
nesting, bad escapes) with an insertion-ordered object model and typed
getters that throw except(). Used by vision-server for its request and
response bodies; covered by test-json.cpp.

Signed-off-by: iamskeole <214513857+iamskeole@users.noreply.github.com>
POSIX-only HTTP/1.1 server (127.0.0.1 by default) serving all five model
families with a minimal JSON API:

  GET  /health
  POST /sam        point/box prompts, all four heads, per-image embed cache
  POST /birefnet   alpha matte
  POST /depth      Depth-Anything-V2
  POST /migan      inpainting (image + mask)
  POST /esrgan     upscaling (optional tiling)

Requests carry base64 images (data: URLs accepted) and support
Expect: 100-continue. On a Vulkan backend the --max-arena reservation is
probed at boot so the server fails loudly instead of starving other
GPU tenants. Covered by test-server.cpp (functional tests that fork the
server, poll /health and exercise the routes and error cases).

Signed-off-by: iamskeole <214513857+iamskeole@users.noreply.github.com>
This ggml-vulkan allocates one buffer per tensor, so there is no arena to
clamp: a graph that does not fit in the remaining device memory fails
mid-allocation, inside another client's headroom.

check_device_budget() dry-runs the graph allocator (ggml_gallocr_reserve_n_size,
no_alloc) to report exactly the buffer the real allocation would create -
in-place ops included, which is why summing tensor sizes overcounts - and
refuses an over-budget request with 503 and its measured figure, before
touching memory. The boot-time arena probe is kept as an early sanity check.

Adds a test: a server with a 16 MB budget refuses a MobileSAM request with
the measured size instead of attempting the allocation.

Signed-off-by: iamskeole <214513857+iamskeole@users.noreply.github.com>
The server's embedding cache captured the encoder's output tensor after
the combined encode+predict graph ran. ggml's allocator may reuse a
parent's buffer for a later in-place op, so by the time the encoder
output was read it could hold intermediate data of the prediction
instead. The next cache-hit request then predicted from that garbage:
the first request of a server process was correct, every subsequent
request on the same image drifted (IoU 0.88 -> 0.29-0.51, never
converging, run-dependent).

Split the request in two sequential graphs:

- phase 1 (encoder only, cache miss): the encoder output is the graph's
  registered output (compute_graph_output), which also registers the
  encoder chain and the image input in the graph so the allocator gives
  them buffers. Nothing can write into the output buffer afterwards, so
  the post-compute host copy is the true encoder output.
- phase 2 (predictor): the embedding is always an input tensor filled
  from that host copy (a few MB), the prompt is encoded in the same
  graph; the masks/iou outputs register it as before.

With the split, repeated requests for the same image are
bit-identical (6/6 point and 3/3 box, CPU and Vulkan) and the
per-request VRAM budget drops to max(encoder, predictor) instead of
their sum: MobileSAM 200 -> 160 MB (Vulkan), peak 883 -> 694 MiB with
all five families resident.

Signed-off-by: iamskeole <214513857+iamskeole@users.noreply.github.com>
iamskeole added a commit to iamskeole/kokon-ee that referenced this pull request Nov 22, 2026
…dency question recorded

Signed-off-by: iamskeole <214513857+iamskeole@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant